﻿using System;

namespace _2_Factory
{
    abstract class shape
    {
        abstract public bool IsLegal();
        abstract public void show();
        abstract public string Name { get; }
        abstract public double Area { get; }
    }
    class Rectangle : shape
    {
        private double length, width;
        override public string Name
        {
            get { return "长方形"; }
        }
        override public double Area
        {
            get { return length * width; }
        }
        public override bool IsLegal()
        {
            return (length > 0 && width > 0);
        }
        public Rectangle(double a, double b)
        {
            length = a; width = b;
            if (!IsLegal()) throw new ArgumentException("该形状不合法");
        }
        public Rectangle() { length = width = 0; }
        public override void show()
        {
            Console.WriteLine("该长方形的长为{0}，宽为{1}", Math.Round(length, 3), Math.Round(width, 3));
        }
    }
    class Square : shape
    {
        override public string Name
        {
            get { return "正方形"; }
        }
        override public double Area
        {
            get { return length * length; }
        }
        private double length;
        public override bool IsLegal()
        {
            return (length > 0);
        }
        public Square(double a) { length = a; if (!IsLegal()) throw new ArgumentException("该形状不合法"); }
        public Square() { length = 0; }
        public override void show()
        {
            Console.WriteLine("该正方形的边长为{0}", Math.Round(length, 3));
        }
    }
    class Triangle : shape
    {
        private double a, b, c;
        override public string Name
        {
            get { return "三角形"; }
        }
        override public double Area
        {
            get { double p = (a + b + c) / 2.0; return Math.Sqrt(p * (p - a) * (p - b) * (p - c)); }
        }
        public override bool IsLegal()
        {
            return (a > 0 && b > 0 && c > 0 && a + b > c && b + c > a && a + c > b);
        }
        public Triangle(double d, double e, double f) { a = d; b = e; c = f; if (!IsLegal()) throw new ArgumentException("该形状不合法"); }
        public Triangle() { a = b = c = 0; }
        public override void show()
        {
            Console.WriteLine("该三角形的边长为{0}、{1}、{2}", Math.Round(a, 3), Math.Round(b, 3), Math.Round(c, 3));
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Random ran = new Random();
            double totalArea = 0.0;
            for (int i = 1; i <= 10; i++)
            {
                int type = ran.Next(1, 4), flag = 1;
                double a = 0, b = 0, c = 0;
                shape thisShape;
                switch (type)
                {
                    case 1:
                        while (flag == 1)
                            try { a = ran.NextDouble() * 10; b = ran.NextDouble() * 10; thisShape = new Rectangle(a, b); flag = 0; thisShape.show(); totalArea += thisShape.Area; }
                            catch { Console.WriteLine("将重新生成一个长方形"); }
                        break;
                    case 2:
                        while (flag == 1)
                            try { a = ran.NextDouble() * 10; thisShape = new Square(a); flag = 0; thisShape.show(); totalArea += thisShape.Area; }
                            catch { Console.WriteLine("将重新生成一个正方形"); }
                        break;
                    case 3:
                        while (flag == 1)
                            try { a = ran.NextDouble() * 10; b = ran.NextDouble() * 10; c = ran.NextDouble() * 10; thisShape = new Triangle(a, b, c); flag = 0; thisShape.show(); totalArea += thisShape.Area; }
                            catch { Console.WriteLine("将重新生成一个三角形"); }
                        break;
                }
            }
            Console.WriteLine("总面积为{0}", Math.Round(totalArea, 3));
        }
    }
}
